<script>


  import BaseCommon from "@/views/base/BaseCommon";

  export default {
    name: "BaseIndex",
    mixins:[BaseCommon],
    data(){
      return {
        // 选中数组
        ids: [],
        // 遮罩层
        loading: false,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roleName: undefined,
          roleKey: undefined,
          status: undefined
        },
        // 表格数据
        list: [],
        // 显示搜索条件
        showSearch: true,
        // bpm的流程路径
        bpmUrl:'',
        showBpmDialog:false,
        isDisabled:false,
      }
    },
    mounted(){
      this.$nextTick(function(){
      })
    },
    activated(){
      if (
        (
          (this.$route.query.type && this.$route.query.type!=='add')
          && (this.$route.query.type && this.$route.query.type!=='edit')
          && (this.$route.query.type && this.$route.query.type!=='change')
        )
      ){
        this.$store.dispatch('tagsView/delCachedView', this.$route)
      }
    },
    methods:{
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
      },
      // 清除缓存
      delCachedViewTo(view,noCache=false){
        if (!noCache){
          this.$store.dispatch('tagsView/delCachedView', view).then(() => {
            this.$nextTick(() => {
              this.$router.push(view)
            })
          })
        }else{
          this.$router.push(view)
        }
      },
      exportFileCommon(params,api) {
        let url = `${process.env.VUE_APP_BASE_API}${api}?1=1`
        for(let key in params){　　//遍历对象的所有属性，包括原型链上的所有属性
          if(params.hasOwnProperty(key) && params[key]) { //判断是否是对象自身的属性，而不包含继承自原型链上的属性
            url += `&${key}=${params[key]}`
          }
        }
        return url
      },
      // 获取状态的中文名字
      getStatusName(status){
        if (status==='active') return '审批中'
        if (status==='completed') return '审批完成'
        if (status==='cancel') return '作废'
        if (status==='reject') return '退回'
        else return '暂存'
      },
      /**
       * 处理传递的参数
       * @param sendForm，发送的对象名字
       * @param baseForm，所有的对象的名字
       * @param filter，过滤 功能 为true的时候就是把sendForm里面的响应的字段进行过滤掉
       */
      dealSendData(sendForm,baseForm,filter=false){
        if (!filter){
          let sendKeys = Object.keys(sendForm)
          for (let i = 0; i < sendKeys.length; i++) {
            let k = sendKeys[i]
            sendForm[k] = baseForm[k];
          }
          return sendForm
        }else{
          let temp = {}
          let baseKeys = Object.keys(baseForm)
          for (let i = 0; i < baseKeys.length; i++) {
            // 表示存在，不保存进去
            if(sendForm[baseKeys[i]] === undefined){
              let k = baseKeys[i]
              temp[k] = baseForm[k];
            }
          }
          return temp
        }
      },
      /**
       * refs:需要去除的 ref
       * ids:需要去除的id
       * minus:需要额外去除的padding的值
       * */
      fillHeight(height,refs,ids,minus=50){
        return this.$nextTick(()=>{
          let refHeight = 0
          if (refs && refs.length){
            for (let i = 0; i < refs.length; i++) {
              let ref = this.$refs[refs[i]]
              if (ref){
                if (ref.$el){
                  refHeight += ref.$el.offsetHeight
                }else{
                  refHeight += ref.offsetHeight
                }
              }
            }
          }
          let otherHeight = 0
          if(ids && ids.length){
            for (let i = 0; i < ids.length; i++) {
              let elementById = document.getElementById(ids[i]);
              if (elementById){
                otherHeight +=  elementById.offsetHeight
              }
            }
          }
          this[height] = parseInt(window.innerHeight - refHeight - otherHeight - minus);
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.$nextTick(function () {
          if (!this.resetData || !this.resetData instanceof Function) {
            return this.msgError('表单重置 必须复写 resetData() 函数') && false
          }
          if (this.$refs[this.$options.name] && this.$refs[this.$options.name].setCheckedKeys) this.$refs[this.$options.name].setCheckedKeys([]);
          else
          if (this.$refs.form && this.$refs.form.setCheckedKeys) this.$refs.form.setCheckedKeys([])
          // 重置form表单的数据
          this.resetData()
          this.resetForm("form");
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加";
      },
      // 能够查看审批
      canAudit(row){
        // 待审核，审核中
        return row.status==='active' || row.status==='completed'
      },
      /** 导出按钮操作 */
      handleExport() {
        let api1 = 'export'+this.$options.name
        let api2 = 'exportList'
        let condition1 = this.getApiName(api1)
        let condition2 = this.getApiName(api2)
        if ( !condition1 && !condition2 ) {
          return this.msgError(`必须实现 ${api1}() 或者 ${api2}()  函数`) && false
        }
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=>{
          if (condition1 && !condition2) return this[api1](queryParams)
          if (condition2 && !condition1) return this[api2](queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },
      getApiName(name){
        return (this[name] && this[name] instanceof Function)
      },
      /** 查询列表 */
      getList() {
        this.$nextTick(()=>{
          this.loading = true;
          let api1 = 'list'+this.$options.name
          let api2 = 'listForm'
          let condition1 = this.getApiName(api1)
          let condition2 = this.getApiName(api2)

          if ( !condition1 && !condition2) {
            return this.msgError(`必须实现 ${api1}() 或者 ${api2}()  函数`) && false
          }
          if (condition1 && !condition2) {
            return this[api1](this.queryParams).then(
              response => {
                if(response && response.code ===0){
                  if (response.data.records)this.list = response.data.records
                  this.total = response.data.total
                }else{
                  this.msgError(response.msg)
                }
                this.loading = false;
              }
            ).catch(err=>{
              this.loading = false;
            });
          }
          if (condition2 && !condition1) {
            return this[api2](this.queryParams).then(
              response => {
                if(response && response.code ===0){
                  if (response.data.records)this.list = response.data.records
                  this.total = response.data.total
                }else{
                  this.msgError(response.msg)
                }
                this.loading = false;
              }
            ).catch(err=>{
              this.loading = false;
            });
          }

        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        let api1 = 'get'+this.$options.name
        let api2 = 'getForm'
        let condition1 = this.getApiName(api1)
        let condition2 = this.getApiName(api2)
        if ( !condition1 && !condition2 ) {
          return this.msgError(`必须实现 ${api1}() 或者 ${api2}()  函数`) && false
        }
        this.reset();
        const tempId = row.id
        this.loading = true
        if (condition1 && !condition2) {
          return this[api1](tempId).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改";
            this.loading = false
          }).catch(err=>{
            this.loading = false
          });
        }
        if (condition2 && !condition1) {
          return this[api2](tempId).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改";
            this.loading = false
          }).catch(err=>{
            this.loading = false
          });
        }

      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let api1 = 'del'+this.$options.name
        let api2 = 'delForm'
        let condition1 = this.getApiName(api1)
        let condition2 = this.getApiName(api2)
        if ( !condition1 && !condition2 ) {
          return this.msgError(`必须实现 ${api1}() 或者 ${api2}()  函数`) && false
        }
        const roleIds = row.id || this.ids;
        let temp = ''
        if (roleIds === row.id) temp = '该'
        if (roleIds === this.ids) temp = '选中的'
        if (!roleIds || (roleIds && !roleIds.length)) return this.msgError(`请选择需要删除的项`) && false
        this.$confirm(`是否确认删除${temp}数据项?`, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=>{
          if (condition1 && !condition2)  return this[api1](roleIds);
          if (!condition1 && condition2)  return this[api2](roleIds);
        }).then(response => {
          if(response && response.code === 0){
            this.getList();
            this.msgSuccess("删除成功");
          }else{
            this.msgError(response.msg)
          }
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              this.loading = true
              let api1 = 'update'+this.$options.name
              let api2 = 'updateForm'
              let condition1 = this.getApiName(api1)
              let condition2 = this.getApiName(api2)
              if ( !condition1 && !condition2 ) {
                return this.msgError(`必须实现 ${api1}() 或者 ${api2}()  函数`) && false
              }
              if (condition1 && !condition2){
                return this[api1](this.form).then(response => {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                  this.loading = false
                }).catch(err=>{
                  this.loading = false
                });
              }
              if (!condition1 && condition2) {
                return this[api2](this.form).then(response => {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                  this.loading = false
                }).catch(err=>{
                  this.loading = false
                });
              }
            } else {
              this.loading = true
              let api1 = 'add'+this.$options.name
              let api2 = 'addForm'
              let condition1 = this.getApiName(api1)
              let condition2 = this.getApiName(api2)
              if ( !condition1 && !condition2 ) {
                return this.msgError(`必须实现 ${api1}() 或者 ${api2}()  函数`) && false
              }
              if (condition1 && !condition2){
                return this[api1](this.form).then(response => {
                  if(response && response.code ===0){
                    this.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                  }else{
                    this.msgError(response.msg)
                  }
                  this.loading = false
                }).catch(err=>{
                  this.loading = false
                });
              }
              if (!condition1 && condition2) {
                return this[api2](this.form).then(response => {
                  if(response && response.code ===0){
                    this.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                  }else{
                    this.msgError(response.msg)
                  }
                  this.loading = false
                }).catch(err=>{
                  this.loading = false
                });
              }
            }
          }
        });
      },
      // 所有节点数据
      getMenuAllCheckedKeys() {
        // 目前被选中的节点
        if (this.$refs[this.$options.name]){
          let checkedKeys = this.$refs[this.$options.name].getCheckedKeys();
          // 半选中的节点
          let halfCheckedKeys = this.$refs[this.$options.name].getHalfCheckedKeys();
          checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
          return checkedKeys;
        }else if (this.$refs.form) {
          let checkedKeys = this.$refs.form.getCheckedKeys();
          // 半选中的节点
          let halfCheckedKeys = this.$refs.form.getHalfCheckedKeys();
          checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
          return checkedKeys;
        }

      },
      goBack() {
        this.$router.go(-1);
      },
    }
  }
</script>
<style lang="scss" scoped>
  @import "./BaseForm";
  .search-container {
    background: #ffffff;
    padding: 10px;
    border-bottom: solid 1px #dddddd;
  }
  .content-container{
    padding: 5px;
    background: #ffffff;
  }
  .app-main-plus{
    background-color: #dddddd;
  }
  // 美化
  .beautifys {
    ::v-deep.el-button{
      border-color: #1890ff;
      text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
      -webkit-box-shadow: 0 2px 0 rgb(0 0 0 / 5%);
      box-shadow: 0 2px 0 rgb(0 0 0 / 5%);
    }
    ::v-deep.el-input--small .el-input__inner:focus {
      text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
      -webkit-box-shadow: 0 0 4px #1890ff55;
      box-shadow: 0 0 4px #1890ff55;
    }
    ::v-deep input:focus {
      text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
      -webkit-box-shadow: 0 0 4px #1890ff55;
      box-shadow: 0 0 4px #1890ff55;
    }
    ::v-deep textarea:focus {
      text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
      -webkit-box-shadow: 0 0 4px #1890ff55;
      box-shadow: 0 0 4px #1890ff55;
    }
  }
</style>

